//
//  HHFullAlphaAnimator.m
//  Pod
//
//  Created by Basic on 2017/3/23.
//  Copyright © 2017年 green. All rights reserved.
//

#import "HHFullAlphaAnimator.h"
#import <BCFoundation/BCFoundationUtils.h>


@interface HHFullAlphaAnimator ()
@property(nonatomic, assign, getter=isPresented) BOOL presented;//判断当前动画是弹出还是消失

@end

@implementation HHFullAlphaAnimator
#pragma mark - system
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)dealloc
{
    BCLogDealloc();
}


#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.presented = YES;
    return self;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.presented = NO;
    return self;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.2;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    if(self.isPresented){
        [self animationForPresentView:transitionContext];
    } else {
        [self animationForDismissView:transitionContext];
    }
}

//自定义弹出动画
- (void)animationForPresentView:(id<UIViewControllerContextTransitioning> )transitionContext
{
//    UIViewController *fromPage = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *presentView = [transitionContext viewForKey:UITransitionContextToViewKey];
    presentView.alpha = 0;
    presentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    [transitionContext.containerView addSubview:presentView];//将执行的View添加到containerView
    if ([self.toDelegate respondsToSelector:@selector(HHFullAlphaAnimatorToWillPresent:)]) {
        [self.toDelegate HHFullAlphaAnimatorToWillPresent:self];
    }
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        presentView.alpha = 1;
        presentView.transform = CGAffineTransformMakeScale(1, 1);
        if ([self.toDelegate respondsToSelector:@selector(HHFullAlphaAnimatorToPresenting:)]) {
            [self.toDelegate HHFullAlphaAnimatorToPresenting:self];
        }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
        if ([self.toDelegate respondsToSelector:@selector(HHFullAlphaAnimatorToCompletion:)]) {
            [self.toDelegate HHFullAlphaAnimatorToCompletion:self];
        }
    }];
}

//自定义消失动画
- (void)animationForDismissView:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIView *dismissView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    if ([self.toDelegate respondsToSelector:@selector(HHFullAlphaAnimatorToWillDismiss:)]) {
        [self.toDelegate HHFullAlphaAnimatorToWillDismiss:self];
    }
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        dismissView.alpha = 0;
//        dismissView.transform = CGAffineTransformMakeScale(0.4, 0.4);
        if ([self.toDelegate respondsToSelector:@selector(HHFullAlphaAnimatorToDismissing:)]) {
            [self.toDelegate HHFullAlphaAnimatorToDismissing:self];
        }
    } completion:^(BOOL finished) {
        [dismissView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
}
@end
